<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>

    <script src="../lib/vue2.js"></script>
  </head>
  <body>
    <div id="box">
      <!-- 通过component标签占位引入任意组件 -->
      <!-- 通过is选择组件名称 -->
      <component is="com-one"></component>
      <!-- 动态绑定is -->

      <button @click="exchange">切换</button>
      <!-- keep-alive 缓存组件 提高性能-->
      <keep-alive>
        <component :is="which"></component>
      </keep-alive>
    </div>

    <script>
      Vue.component("comOne", {
        template: `<div>组件1</div>`,
      });
      Vue.component("comTwo", {
        template: `<div>组件2</div>`,
      });

      new Vue({
        el: "#box",
        data() {
          return {
            which: "com-one",
          };
        },

        methods: {
          exchange() {
            if (this.which === "com-one") this.which = "com-two";
            else this.which = "com-one";
          },
        },
      });
    </script>
  </body>
</html>
